package 字符串;

/**
 * @Author Weton Li
 * @Date 2021/8/25 21:50
 */
public class 删除最外层括号 {
    public static void main(String[] args) {
        String s = "(()())(())";
        System.out.println(removeOuterParentheses(s));
    }

    public static String removeOuterParentheses(String s) {
        char[] chars = s.toCharArray();
        int x = 0;
        String res = "";
        for (int i = 0; i < chars.length; i++) {
            if (s.charAt(i) == '(') {
                x++;
                if (x == 1) {
                    continue;
                }
            } else {
                x--;
                if (x == 0) {
                    continue;
                }
            }
            res += chars[i];
        }
        return res;
    }
}
